package tools;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;


public class Bimg {
	
	/**
	 * Returns a BufferedImage representation of the desired image, found through loader.getResource(filePath)
	 * 
	 * @param loader 		The ClassLoader which can access the file located at file path
	 * @param filePath		The file path of the desired image, relative to the project running
	 * @param canvas		A "blank" BufferedImage, set up with the desired type.
	 * @return				A BufferedImage
	 */
	public static BufferedImage getBimg(ClassLoader loader, String filePath, BufferedImage canvas)
	{
		ImageIcon img = new ImageIcon(loader.getResource(filePath));
		Graphics2D g = canvas.createGraphics();
		g.drawImage(img.getImage(), 0, 0, null);
		
		return canvas;
	}
	
	/**
	 * Returns a BufferedImage representation of the desired image, found through loader.getResource(filePath)
	 * 
	 * @param loader 		The ClassLoader which can access the file located at file path
	 * @param filePath		The file path of the desired image, relative to the project running
	 * @param canvas		A "blank" BufferedImage. Must be BufferedImage.TYPE_ARGB.
	 * @param transparentRGBColor	The color to be transparent in the returned image
	 * @return				A BufferedImage
	 */
	public static BufferedImage getBimg(ClassLoader loader, String filePath, BufferedImage canvas, int transparentRGBColor)
	{
		BufferedImage bimg = getBimg(loader, filePath, canvas);
		
		for(int x = 0; x < bimg.getWidth(); x++) {
			for(int y = 0; y < bimg.getWidth(); y++) {
				if(bimg.getRGB(x, y) == transparentRGBColor) {
					int newTransparent = transparentRGBColor & 0x00FFFFFF;
					bimg.setRGB(x, y, newTransparent);
				}
			}
		}
		
		return bimg;
	}
	
	/**
	 * Returns a BufferdImage representation of img, drawn upen the canvas
	 * 
	 * @param img		The ImageIcon to be "converted" to a BufferedImage
	 * @param canvas	The BufferedImage to contain img
	 * @return			A BufferedImage
	 */
	public static BufferedImage getBimg(ImageIcon img, BufferedImage canvas)
	{
		Graphics2D g = canvas.createGraphics();
		g.drawImage(img.getImage(), 0, 0, null);
		
		return canvas;
	}
	
	/**
	 * Returns a BufferdImage representation of img with the type BufferedImage.TYPE_INT_ARGB
	 * 
	 * @param img		The ImageIcon to be "converted" to a BufferedImage
	 * @return			A BufferedImage
	 */
	public static BufferedImage getBimg(ImageIcon img)
	{
		BufferedImage bimg = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
		return getBimg(img, bimg);
	}
	
	/**
	 * Returns a BufferdImage representation of img with the type BufferedImageType
	 * 
	 * @param img		The ImageIcon to be "converted" to a BufferedImage
	 * @return			A BufferedImage
	 */
	public static BufferedImage getBimg(ImageIcon img, int bufferedImageType)
	{
		BufferedImage bimg = new BufferedImage(img.getIconWidth(), img.getIconHeight(), bufferedImageType);
		return getBimg(img, bimg);
	}
}
